﻿using Aker.Cikislar;
using Aker.DevTeam;
using Aker.MainForms;
using Aker.Product;
using Aker.Properties;
using Cikislar;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Aker.Uretim
{
    public partial class SiparisTakipVeCikis : XForm
    {
        DataTable dt;
        string[] editAbleColumns;
        int SiparisTipi;
        string[] orderedColumns;
        int POId = -1;

        public SiparisTakipVeCikis()
        {
            InitializeComponent();
            SiparisTipi = 1;
            orderedColumns = new string[] {
                                                    "POrderId",
                                                    "POrderLinesId",                                                                                              
                                                    "OrderDate",
                                                    "Brand",
                                                    "SiparisiAlan",
                                                    "ETA",
                                                    "KalanGun",
                                                    "PaymentType",                                                    
                                                    "Notes",
                                                    "Status",
                                                    "ProductId",
                                                    "MusteriKodu",
                                                    "SentezKodu",
                                                    //"Artikel",
                                                    //"PartiNo",
                                                    "BazArtikel",
                                                    "BazArtikelPartiNo",
                                                    "Thumb",                                                     
                                                    "Design",
                                                    "Weight",
                                                    "Width",
                                                    "Composition",  
                                                    "Qty",  
                                                    "QtyIade",
                                                    "QtyIskonto",
                                                    "Price",                                                                                                     
                                                    "SatirNotu",                                                    
                                                    "TotalTransfered",
                                                    "NewTransferMT",
                                                    "NewTransferKG",
                                                    "TopAdedi",
                                                    //"MalCinsi",
                                                    "Ü",
                                                    "T",                                                   
                                                    };
        }

        public SiparisTakipVeCikis(int SiparisTipi, int POId = -1)
        {
            InitializeComponent();
            this.SiparisTipi = SiparisTipi;
            orderedColumns = new string[] {
                                                    "POrderId",
                                                    "POrderLinesId",                                                                                              
                                                    "OrderDate",
                                                    "Brand",
                                                    "Grup",
                                                    "SiparisiAlan",
                                                    "ETA",
                                                    "KalanGun",
                                                    "PaymentType",                                                    
                                                    "Notes",
                                                    "Status", 
                                                    "BirimAdet",
                                                    "Qty",   
                                                    "Price",                                                                                                     
                                                    "SatirNotu",                                                    
                                                    "TotalTransfered",                                                 
                                                    "NewTransferKG",
                                                    "TopAdedi",
                                                    //"MalCinsi",
                                                    "Ü",
                                                    "T",                                                   
                                                    };

            this.POId = POId;
        }

        private void Orders_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            gc.Enabled = false;
            if (Data.IsLookupInitialized)
                Bind();
            else
                Data.LookupInitialized += Bind;
        }

        private void Bind()
        {
            editAbleColumns = new string[] { "NewTransferKG", "NewTransferMT", "TopAdedi", "MalCinsi", "MusteriKodu", "SentezKodu" };

            //if (SiparisTipi == 1)
            //    editAbleColumns = new string[] { "NewTransferKG", "NewTransferMT", "TopAdedi", "MalCinsi", "MusteriKodu" };
            //else
            //    editAbleColumns = new string[] { "NewTransferKG", "NewTransferMT", "TopAdedi", "MalCinsi", "MusteriKodu" };

            dt = Data.DM.ExecuteDataSet("GetPOrders", false, SiparisTipi).Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                int bazProductId = dr["BazProductId"].ToIntNN();
                if (bazProductId > -1)
                {
                    DataRow pr = Data.DB.Product.Select("ProductId = " + bazProductId)[0];

                    foreach (string col in Data.ProductDetails)
                    {
                        if (!dt.Columns.Contains(col))
                            dt.Columns.Add(col, typeof(string));
                        if (pr != null)
                            dr[col] = pr[col];
                    }
                }
            }

            dt.AcceptChanges();

            gv.Init(dt);


            RepositoryItemMemoEdit riMemo = new RepositoryItemMemoEdit();
            gv.Columns["Notes"].ColumnEdit = riMemo;
            gv.Columns["Notes"].AppearanceCell.TextOptions.VAlignment = VertAlignment.Center;

            GridColumn gcThumb = new GridColumn();
            gcThumb.Name = "Thumb";
            gcThumb.Caption = "";
            gcThumb.FieldName = "Thumb";
            gcThumb.VisibleIndex = 0;
            gv.Columns.Add(gcThumb);

            gv.SetVisibleIndex(orderedColumns);

            gv.SetCaptions("POrderId:Sip.No", "OrderDate:Sipariş Tarihi", "Brand:Müşteri", "SiparisiAlan:Siparişi Alan", "KalanGun:Kalan Gün", "ToplamArtikel:Artikel Adedi", "PaymentType:Ödeme Şekli",
                "Status:Durum", "ETA:Termin", "Notes:Notlar", "BazArtikelPartiNo:Baz Parti No", "Qty:Miktar", "QtyIade:İade Miktarı", "QtyIskonto:İskonto Tutarı", "Grup:İplik Grup", "SatirNotu:Not");

            gv.SetCaptions("ProductId:Artikel Id", "Thumb:Resim", "Design:Desen", "Composition:Karışm", "Width:En", "Weight:Ağırlık", "Notes:Notlar",
                    "Price:Fiyat", "TotalTransfered:Toplam Çıkılan", "NewTransferMT:Çıkılacak MT", "NewTransferKG:Çıkılacak KG", "TopAdedi:Adet", "Status:Durum"
                    );


            gv.MakeReadOnly(editAbleColumns);
            gv.FormatAsNumber("TotalTransfered", "QtyIade", "NewTransferKG", "NewTransferMT", "QtyIskonto");
            gv.MakeColumnInvisible("POrderLinesId", "ProductId", "CreatorId");

            gv.BestFitColumns();

            gv.Columns["Notes"].Width = gv.Columns["SatirNotu"].Width = 250;
            gv.Columns["Status"].Width = 100;

            if (!AppSession.UserInfo.Yonetici)
            {
                gv.Columns["Price"].Visible = false;
                gv.Columns["Price"].OptionsColumn.ShowInCustomizationForm = false;
            }

            gv.OptionsView.ShowViewCaption = false;

            gv.MakeColumnInvisible("Ü", "T", "StatusId");

            gv.RowCellStyle += gv_RowCellStyle;
            gv.CustomDrawCell += gv_CustomDrawCell;
            gv.CustomDrawCell += gvDetail_CustomDrawCell;
            gv.CalcRowHeight += gv_CalcRowHeight;
            gv.CellValueChanged += gv_CellValueChanged;
            gv.RowCellClick += gv_RowCellClick;
            gv.RowStyle += gv_RowStyle;

            GridManager gm = GridManager.GetManager(gv);

            gm.GridMenu.AddMenu("Aynı artikeli üret", AyniArtikel, Resources.Ok, true);
            gm.GridMenu.AddMenu("Yeni artikel oluştur", KopyaArtikel, Resources.Copy);
            gm.GridMenu.AddMenu("Renk varyantı oluştur", RenkVaryanti, Resources.Color2);

            gm.GridMenu.AddMenu("Siparişi Düzenle", EditOrder, true);
            gm.GridMenu.AddMenu("Siparişi Yazdır", PrintOrder);

            gm.GridMenu.AddMenu("İade Et", Iade, Resources.miDelete, true);

            if (SiparisTipi == 2)
            {
                this.Text = this.Text.Replace("Üretim", "İplik");
                btnCik.Caption = "Çıkış Emri Oluştur";
                btnSepeteEkle.Enabled = false;
                btnSepeteGit.Enabled = false;
                if (POId > -1)
                {
                    this.Text = "İplik Siparişi:" + POId.ToString();
                    gv.ActiveFilterString = "POrderId=" + POId.ToString();
                    for (var i = 0; i < gv.RowCount; i++)
                    {
                        DataRow dr = gv.GetDataRow(i);
                        dr["NewTransferKG"] = dr["KG"];
                    }
                }
            }

            gc.Enabled = true;
            gc.Refresh();
            gv.RefreshData();
        }

        public void RefreshData()
        {
            int POrderId = gv.GetFocusedRowCellValue("POrderId").ToIntNN();
            BindData();
            int rh = gv.LocateByValue("POrderId", POrderId);
            gv.FocusedRowHandle = rh;
        }

        private void Iade(object sender, EventArgs e)
        {
            DataRow dr = gv.GetFocusedDataRow();
            int pOrderLinesId = dr["POrderLinesId"].ToIntNN();
            using (Iade iade = new Iade(pOrderLinesId))
            {
                if (iade.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    dr["QtyIade"] = dr["QtyIade"].ToIntNN(0) + iade.Qty;
                    dr["QtyIskonto"] = dr["QtyIskonto"].ToIntNN(0) + iade.Iskonto;
                }
            }
        }

        private void PrintOrder(object sender, EventArgs e)
        {
            DataRow dr = gv.GetFocusedDataRow();
            int podId = dr["POrderId"].ToIntNN();
            int langId = dr["LangId"].ToIntNN(); ;
            string email = dr["Email"].ToStringNN();

            SiparisFormu report = new SiparisFormu(podId, langId);
            AppSession.MainForm.ShowMdiChildForm(typeof(ReportViewer), report, email, "Sipariş No:" + podId);
        }

        private void EditOrder(object sender, EventArgs e)
        {
            if (gv.FocusedRowHandle > -1)
            {
                DataRow dr = gv.GetFocusedDataRow();

                int podId = dr["POrderId"].ToIntNN();
                int userId = dr["CreatorId"].ToIntNN();
                string status = dr["Status"].ToStringNN();
                int statusId = Data.DB.Status.Select("Status='" + status + "'")[0]["StatusId"].ToIntNN();

                if (AppSession.UserInfo.Yonetici || AppSession.UserInfo.YetkiId == 4)
                    AppSession.MainForm.ShowMdiChildForm(typeof(YeniSiparis), podId);
                else if (userId == AppSession.UserInfo.UserId)
                    if (statusId == 1)
                        AppSession.MainForm.ShowMdiChildForm(typeof(YeniSiparis), podId);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Onaylanan siparişleri sadece yönetim değiştirebilir !", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Sadece kendi oluşturduğunuz siparişleri değiştirebilirsiniz.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                DevExpress.XtraEditors.XtraMessageBox.Show("Sipariş satırı üzerinde tıklayınız.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void RenkVaryanti(object sender, EventArgs e)
        {
            UretimeAl(2);
        }

        private void KopyaArtikel(object sender, EventArgs e)
        {
            UretimeAl(1);
        }

        private void AyniArtikel(object sender, EventArgs e)
        {
            UretimeAl(0);
        }


        private void UretimeAl(int type)
        {
            DataRow dr = gv.GetFocusedDataRow();
            string bazArtikel = dr["BazArtikel"].ToStringNN();
            string partino = dr["BazArtikelPartiNo"].ToStringNN();
            if (partino != "")
                bazArtikel = bazArtikel + (bazArtikel.Contains("-") ? "" : "-") + partino;

            int pOrderLinesId = dr["POrderLinesId"].ToIntNN();

            bool isKG = dr["Qty"].ToStringNN().Contains("kg");
            decimal qty = dr["Meters"].ToDecimalNN();
            if (isKG)
                qty = dr["KG"].ToDecimalNN();

            int stokKartiTipi = dr["StokKartiTipi"].ToIntNN();
             
            //TODO: tipi kendi belirleyebilir
            if (stokKartiTipi == -1)
                stokKartiTipi = StokKartiTipiSecici.GetStokKartiTipi(bazArtikel);


            if (stokKartiTipi == 1)
            {
                YeniImalatKarti1 sk = AppSession.MainForm.ShowMdiChildForm(typeof(YeniImalatKarti1), bazArtikel, type, qty, isKG) as YeniImalatKarti1;
                sk.ProductInserted += sk_ProductInserted;
            }
            else if (stokKartiTipi == 2)
            {
                YeniImalatKarti2 sk = AppSession.MainForm.ShowMdiChildForm(typeof(YeniImalatKarti2), bazArtikel, type, qty, isKG) as YeniImalatKarti2;
                sk.ProductInserted += sk_ProductInserted;
            }
        }

        void sk_ProductInserted(int ProductId, string Artikel, int StokKartiTipi)
        {
            int porderlinesId = gv.GetFocusedRowCellValue("POrderLinesId").ToIntNN();
            Data.DM.ExecuteNonQuery("UpdPOrderLinesProduct", porderlinesId, ProductId, StokKartiTipi);
            gv.SetFocusedRowCellValue("Artikel", Artikel);
            gv.SetFocusedRowCellValue("StokKartiTipi", StokKartiTipi);

            AppSession.MainForm.ShowMdiChildForm(typeof(ImalatKartlari), ProductId);
        }

        void gv_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            //if (e.RowHandle == gv.FocusedRowHandle)
            //    e.Appearance.BackColor = Color.LightYellow;

            if (gv.GetDataRow(e.RowHandle)["Status"].ToStringNN() == "İptal")
            {
                e.Appearance.BackColor = Color.LightCyan;
                e.Appearance.FontStyleDelta = FontStyle.Strikeout;
            }
        }

        void gv_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "Status" && e.RowHandle > -1)
            {
                int u = gv.GetRowCellValue(e.RowHandle, "Ü").ToIntNN();
                int t = gv.GetRowCellValue(e.RowHandle, "T").ToIntNN();

                Brush ellipseBrush = Brushes.LightSkyBlue;
                Rectangle r = e.Bounds;


                if (u > 0 || t > 0)
                {
                    decimal pay = r.Width / Convert.ToDecimal(u + t);

                    if (t > 0)
                    {

                        int a = 0;
                        a++;
                    }

                    RectangleF ru = new RectangleF((float)r.X, (float)r.Y, (float)pay * u, (float)r.Height);
                    ellipseBrush = Brushes.Yellow;
                    e.Graphics.FillRectangle(ellipseBrush, ru);
                    e.Appearance.DrawString(e.Cache, "Ü", new Rectangle(r.X + 3, r.Y, r.Width - 12, r.Height));

                    ru = new RectangleF((float)r.X + (float)ru.Width, (float)r.Y, (float)pay * t, (float)r.Height);
                    ellipseBrush = Brushes.Aqua;
                    e.Graphics.FillRectangle(ellipseBrush, ru);
                    e.Appearance.DrawString(e.Cache, "T", new Rectangle(ru.X.ToIntNN() + 3, ru.Y.ToIntNN(), ru.Width.ToIntNN() - 12, ru.Height.ToIntNN()));
                }
                else
                {
                    if (e.DisplayText == "Onaylandı")
                        ellipseBrush = Brushes.LightGreen;
                    else
                        ellipseBrush = Brushes.White;

                    e.Graphics.FillRectangle(ellipseBrush, r);

                    r.Width -= 12;
                    e.Appearance.DrawString(e.Cache, e.DisplayText, r);
                }

                e.Handled = true;
            }
        }


        void gv_CalcRowHeight(object sender, RowHeightEventArgs e)
        {
            if (!gv.IsFilterRow(e.RowHandle))
                e.RowHeight = 50;
        }

        void gv_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            gv.CellValueChanged -= gv_CellValueChanged;

            if (e.Column.FieldName.ToLower().Contains("new"))
            {
                DataRow dr = gv.GetDataRow(e.RowHandle);
                decimal qtySipMt = (decimal)dr["Meters"].ToDecimalNN();
                decimal qtySipKG = (decimal)dr["KG"].ToDecimalNN();

                decimal qtyNewMt = (decimal)dr["NewTransferMT"].ToDecimalNN();
                decimal qtyNewKG = (decimal)dr["NewTransferKG"].ToDecimalNN();

                decimal qtyOld = (decimal)dr["TotalTransfered"].ToDecimalNN();

                if (qtySipMt > 0 && qtyNewKG > 0)
                {
                    gv.SetRowCellValue(e.RowHandle, "NewTransferKG", null);
                    ShowError("Talep cinsi ile sevkiyat aynı cins olmalı !");
                }
                else if (qtySipKG > 0 && qtyNewMt > 0)
                {
                    gv.SetRowCellValue(e.RowHandle, "NewTransferMT", null);
                    ShowError("Talep cinsi ile sevkiyat aynı cins olmalı !");
                }
                else
                {
                    //decimal qtyNew = (decimal)e.Value;
                    //if (qtyNew + qtyOld > (qtySipMt + qtySipKG) * Convert.ToDecimal(1.05))
                    //{
                    //    ShowError("Talep edilen miktarın ± %5'i kabul edilebilir !");
                    //    gv.SetRowCellValue(e.RowHandle, "NewTransferKG", null);
                    //    gv.SetRowCellValue(e.RowHandle, "NewTransferMT", null);
                    //}
                }
            }
            else if (e.Column.FieldName.ToLower() == "sentezkodu")
            {
                DataRow dr = gv.GetDataRow(e.RowHandle);
                int porderLinesId = dr["POrderLinesId"].ToIntNN();
                string sentezKodu = dr["SentezKodu"].ToStringNN();

                Data.DM.ExecuteNonQuery("UpdPOrderLines", porderLinesId, sentezKodu);
            }

            gv.CellValueChanged += gv_CellValueChanged;
        }



        void gvDetail_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "Thumb" && e.RowHandle > -1)
            {
                int bazProductId = gv.GetRowCellValue(e.RowHandle, "BazProductId").ToIntNN();

                if (bazProductId > -1)
                {
                    AkerDB.ProductRow pr = Data.DB.Product.FindByProductId(bazProductId);

                    if (!pr.IsThumbnailNull())
                    {
                        MemoryStream ms = new MemoryStream(pr.Thumbnail);
                        e.Graphics.DrawImage(Image.FromStream(ms), e.Bounds.X, e.Bounds.Y);
                        e.Handled = true;
                    }
                }
            }
        }

        void gv_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "Thumb")
            {
                int bazProductId = (int)gv.GetFocusedRowCellValue("BazProductId");

                AkerDB.ProductRow pr = Data.DB.Product.FindByProductId(bazProductId);

                if (!pr.IsThumbnailNull())
                {
                    ImageViewer iv = new ImageViewer(bazProductId);
                    iv.ShowDialog();
                }
            }
        }

        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            if (e.RowHandle == gv.FocusedRowHandle)
                e.Appearance.BackColor = Color.LightSalmon;

            if (e.RowHandle > -1 && editAbleColumns.Contains(e.Column.FieldName))
                e.Appearance.BackColor = Color.LightYellow;



            if (e.Column.FieldName == "Status")
            {
                if (e.CellValue.ToStringNN() == "Onay Bekleniyor")
                    e.Appearance.BackColor = Color.White;
                else if (e.CellValue.ToStringNN() == "Onaylandı")
                    e.Appearance.BackColor = Color.LightGreen;
                else if (e.CellValue.ToStringNN() == "Üretim")
                    e.Appearance.BackColor = Color.Yellow;
                else if (e.CellValue.ToStringNN() == "Tamamlandı")
                    e.Appearance.BackColor = Color.Aqua;
                else if (e.CellValue.ToStringNN() == "İptal")
                {
                    e.Appearance.BackColor = Color.LightCyan;
                    e.Appearance.FontStyleDelta = FontStyle.Strikeout;
                }
            }

            else if (e.Column.FieldName == "Tutar")
                e.Appearance.FontStyleDelta = FontStyle.Bold;
            else if (e.Column.FieldName == "KalanGun")
            {
                int kalanGun = e.CellValue.ToIntNN();

                if (kalanGun == 0)
                    e.Appearance.ForeColor = Color.White;
                else if (e.CellValue.ToIntNN() < 0)
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                    e.Appearance.FontStyleDelta = FontStyle.Bold;
                }
            }
            else if (e.Column.FieldName.Contains("BazArtikel"))
            {
                e.Appearance.FontStyleDelta = FontStyle.Strikeout;
                e.Appearance.ForeColor = Color.Red;
            }
        }

        private bool Kontrol()
        {
            gv.EndEditing();

            DataTable changes = dt.GetChanges();
            if (changes != null && changes.Rows.Count > 0)
                return true;
            else
            {
                ShowInfo("Değişiklik yok.");
                return false;
            }
        }

        private void btnCik_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Kontrol())
            {

                DataTable changes = dt.GetChanges();
                int cariId = changes.Compute("Max(CustomerId)", "").ToIntNN();
                int teslimatCariId = changes.Compute("Max(DeliveryCustomerId)", "").ToIntNN();

                if (SiparisTipi == 2)
                {
                    foreach (DataRow dr in changes.Rows)
                    {
                        AkerDB.CikisEmriLinesRow ce = Cikis.YeniCikisEmriSatiri();

                        ce.POrderLinesId = dr["POrderLinesId"].ToIntNN();

                        if (dr["NewTransferKG"].ToDecimalNN() > 0)
                            ce.KG = dr["NewTransferKG"].ToDecimalNN();

                        if (dr["TopAdedi"].ToIntNN() > 0)
                            ce.Adet = dr["TopAdedi"].ToIntNN();

                        Cikis.CikisEmrineEkle(ce);
                    }

                    if (Cikis.CikisEmriOlustur(5, cariId) == System.Windows.Forms.DialogResult.OK)
                    {
                        BindData();
                        ShowInfo("Çıkış başarıyla gerçekleştirildi.");
                    }
                }
                else
                {
                    Cikis.TamponuTemizle();
                    foreach (DataRow dr in changes.Rows)
                    {
                        AkerDB.CikisLinesRow cikisLines = Cikis.YeniTamponSatiri();

                        cikisLines.POrderLinesId = dr["POrderLinesId"].ToIntNN();

                        if (dr["NewTransferKG"].ToDecimalNN() > 0)
                            cikisLines.CikilanKG = dr["NewTransferKG"].ToDecimalNN();

                        if (dr["NewTransferMT"].ToDecimalNN() > 0)
                            cikisLines.CikilanMetre = dr["NewTransferMT"].ToDecimalNN();

                        if (dr["TopAdedi"].ToIntNN() > 0)
                            cikisLines.CikilanAdet = dr["TopAdedi"].ToIntNN();

                        Cikis.TamponaEkle(cikisLines);
                    }

                    if (Cikis.TamponuCik(3, cariId) == System.Windows.Forms.DialogResult.OK)
                    {
                        BindData();
                        ShowInfo("Çıkış başarıyla gerçekleştirildi.");
                    }
                }
            }

            //gv.EndEditing();
            //if (dt.GetChanges() != null)
            //{
            //    DataTable dtChanges = dt.GetChanges().DefaultView.ToTable(false, "POrderLinesId", "NewTransferMT", "NewTransferKG");
            //    dtChanges.TableName = "Transfer";
            //    string xml = dtChanges.Serialize();
            //    DataTable result = Data.DM.ExecuteDataTable("InsTransfer", xml, AppSession.UserInfo.UserId);
            //    int sevkiyat = result.Rows[0][0].ToIntNN();
            //    SevkiyatRaporu tr = new SevkiyatRaporu(sevkiyat, 1);

            //    AppSession.MainForm.ShowMdiChildForm(typeof(ReportViewer), tr, "", "Sevkiyat No:" + sevkiyat);

            //    RefreshData();
            //}
            //else
            //    ShowInfo("Lütfen çıkış yapılacak sipariş satırlarının karşılarına miktar yazın");
        }


        private void btnYenile_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            BindData();

            //gv.BestFitColumns();

            //gv.Columns["Notes"].Width = 250;
            //gv.Columns["SatirNotu"].Width = 250;
            //gv.Columns["Status"].Width = 100;
        }

        private void bbiShowCancelled_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (bbiShowCancelled.Checked && bbiOK.Checked)
                gv.Columns["Status"].ClearFilter();
            else
            {
                List<string> filters = new List<string>();

                if (!bbiShowCancelled.Checked)
                    filters.Add("Status != 'İptal'");

                if (!bbiOK.Checked)
                    filters.Add("Status != 'Tamamlandı'");

                string filter = string.Join(" and ", filters.ToArray());

                gv.Columns["Status"].FilterInfo = new ColumnFilterInfo(filter);
            }
        }

        private void btnSepeteEkle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Kontrol())
            {
                DataTable changes = dt.GetChanges();
                foreach (DataRow dr in changes.Rows)
                {
                    AkerDB.CikisLinesRow cikisLines = CikisSepet.YeniCikisLines();

                    cikisLines.POrderLinesId = dr["POrderLinesId"].ToIntNN();

                    if (dr["NewTransferKG"].ToDecimalNN() > 0)
                        cikisLines.CikilanKG = dr["NewTransferKG"].ToDecimalNN();

                    if (dr["NewTransferMT"].ToDecimalNN() > 0)
                        cikisLines.CikilanMetre = dr["NewTransferMT"].ToDecimalNN();

                    if (dr["TopAdedi"].ToIntNN() > 0)
                        cikisLines.CikilanAdet = dr["TopAdedi"].ToIntNN();

                    cikisLines["SipNo"] = dr["POrderId"].ToStringNN();
                    cikisLines["Müşteri"] = dr["Brand"];

                    if (SiparisTipi == 1)
                    {
                        //cikisLines["ProductId"] = dr["BazProductId"].ToIntNN();
                        cikisLines["Artikel"] = dr["Artikel"];
                        cikisLines["Desen"] = dr["Design"];
                        cikisLines["Karışım"] = dr["Composition"];
                        cikisLines["Ağırlık"] = dr["Weight"];
                    }
                    else if (SiparisTipi == 2)
                    {
                        //TODO : Bu kısım eksik
                        cikisLines["Aciklama"] = dr["Grup"];
                    }


                    //cikisLines.ProductId = dr["ProductId"].ToIntNN();
                    //cikisLines["Artikel"] = dr["Artikel"];
                    //cikisLines["Desen"] = dr["Desen"];
                    //cikisLines["Karışım"] = dr["Karışım"];
                    //cikisLines["Ağırlık"] = dr["Ağırlık"];


                    CikisSepet.SepeteAt(cikisLines);
                }

                btnTemizle.PerformClick();
                ShowInfo("Sepete atıldı.");
            }
        }

        private void btnSepeteGit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AppSession.MainForm.ShowMdiChildForm(typeof(Cikislar.CikisSepet));
        }

        private void btnTemizle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();
            foreach (DataRow dr in dt.Select("isnull(NewTransferKG,0) > 0 or isnull(NewTransferMT,0) > 0 or isnull(TopAdedi,0) > 0"))
            {
                dr["NewTransferKG"] = DBNull.Value;
                dr["NewTransferMT"] = DBNull.Value;
                dr["TopAdedi"] = DBNull.Value;
            }
            dt.AcceptChanges();
            //BindData();
        }



    }
}